/**
 * Created by yuris on 04.12.14.
 */
package controller {
import flash.geom.Point;

import messages.DebugMessage;

import model.Constants;
import model.RuntimeProxy;
import model.UserProxy;
import model.objects.ShopDesignData;
import model.objects.ShopDesignData;
import model.objects.ShopObjectData;

import mvcexpress.mvc.Command;

public class UnpackShopDesignCommand extends Command{
    [Inject]
    public var runtimeProxy:RuntimeProxy;

    [Inject]
    public var userProxy:UserProxy;
    public function execute(param:Object):void {
        switch (param.type){
            case "user":
                    userProxy.shopDesign = _unpackShopDesign(param.data);
                    if(runtimeProxy.shopDesign == null){
                        runtimeProxy.shopDesign = userProxy.shopDesign;
                    }
                break;
        }
        //runtimeProxy.shopDesign = _unpackShopDesign(param.data);
    }


//    _userProxy.shopDesignPacked = {
//        sizeX:10,sizeY:8,
//        floors:{x5y6:"2",x0y0:"2", std:"Floor1"},
//        walls:{x1:"w1",x3:"d1",y2:"d1", std:"w1"},
//        covers:{x1:"w1",y4:"w1"}  //wall covers (windows, etc)
//    };
    private function _unpackShopDesign(packedDesign:Object):ShopDesignData {
        var _stdWall:String = Constants.PATH_WALL_SPRITES+"/"+packedDesign.walls.std;
        var _stdFloor:String = Constants.PATH_FLOOR_SPRITES+"/"+packedDesign.floors.std;

        var _result:ShopDesignData = new ShopDesignData(packedDesign.sizeX,packedDesign.sizeY,_stdFloor,_stdWall);

        var bufName:String;
        var orient:String;
        var fullSpriteName:String;
        var bufValue:String;
        var _bufInt:int;

        for (bufName in packedDesign.walls){
            if(bufName != "std"){


                bufValue = packedDesign.walls[bufName];

                fullSpriteName = Constants.PATH_WALL_SPRITES+"/Wall" + bufValue;

                orient = bufName.charAt(0);
                _bufInt = int(bufName.substring(1,bufName.length));

                switch (orient.toLowerCase()){
                    case "x":
                        if(!_result.quadIsInside(_bufInt,0)){
                            sendMessage(DebugMessage.ERROR_MESSAGE, "UnpackShopDesignCommand: " +
                                    "Quad ["+_bufInt+",0] is outside the shop!")
                        } else {
                            _result.setWallAtX(_bufInt,fullSpriteName);
                        }
                        break;
                    case "y":
                        if(!_result.quadIsInside(0,_bufInt)){
                            sendMessage(DebugMessage.ERROR_MESSAGE, "UnpackShopDesignCommand: " +
                                    "Quad [0,"+_bufInt+"] is outside the shop!")
                        } else {
                            _result.setWallAtY(_bufInt,fullSpriteName);
                        }
                        break;
                }

            }
        }

        for (bufName in packedDesign.doors){
            bufValue = packedDesign.doors[bufName];

            fullSpriteName = Constants.PATH_DOOR_SPRITES+"/Door" + bufValue;

            orient = bufName.charAt(0);
            _bufInt = int(bufName.substring(1,bufName.length));

            switch (orient.toLowerCase()){
                case "x":
                    if(!_result.quadIsInside(_bufInt,0)){
                        sendMessage(DebugMessage.ERROR_MESSAGE, "UnpackShopDesignCommand: " +
                                "Quad ["+_bufInt+",0] is outside the shop!")
                    } else {
                        _result.setDoorAtX(_bufInt,fullSpriteName);
                    }
                    break;
                case "y":
                    if(!_result.quadIsInside(0,_bufInt)){
                        sendMessage(DebugMessage.ERROR_MESSAGE, "UnpackShopDesignCommand: " +
                                "Quad [0,"+_bufInt+"] is outside the shop!")
                    } else {
                        _result.setDoorAtY(_bufInt,fullSpriteName);
                    }
                    break;
            }
        }

        var bufPoint:Point = new Point();
        for (bufName in packedDesign.floors){
            if(bufName != "std"){
                bufPoint = ShopObjectData.convertPosition(bufName);
                bufValue = packedDesign.floors[bufName];
                if(bufValue != null){
                    fullSpriteName = Constants.PATH_FLOOR_SPRITES+"/Floor" + bufValue;

                    if(!_result.quadIsInside(bufPoint.x,bufPoint.y)){
                        sendMessage(DebugMessage.ERROR_MESSAGE, "UnpackShopDesignCommand: " +
                                "Quad ["+bufPoint.x+","+bufPoint.y+"] is outside the shop!")
                    } else {
                        _result.setFloorAt(bufPoint,fullSpriteName);
                    }

                }
            }
        }


        var _bufArr:Array;
        for (bufName in packedDesign.covers){

            orient = bufName.charAt(0);
            bufValue = packedDesign.covers[bufName];

            fullSpriteName = null;
            if(bufValue){
                _bufArr = bufValue.split('_');
                _bufInt = _bufArr[1];

                switch (_bufArr[0]){
                    case Constants.TYPE_WINDOW://window
                        fullSpriteName = Constants.PATH_WINDOW_SPRITES+"/Window" + _bufInt;
                        break;

                }
            }
            _bufInt = int(bufName.substring(1,bufName.length));
            if(fullSpriteName != null && fullSpriteName != ""){
                switch (orient.toLowerCase()){
                    case "x":
                        if(!_result.quadIsInside(_bufInt,0)){
                            sendMessage(DebugMessage.ERROR_MESSAGE, "UnpackShopDesignCommand: " +
                                    "Quad ["+_bufInt+",0] is outside the shop!")
                        } else {
                            _result.setWallCoverAtX(_bufInt,fullSpriteName);
                        }
                        break;
                    case "y":
                        if(!_result.quadIsInside(0,_bufInt)){
                            sendMessage(DebugMessage.ERROR_MESSAGE, "UnpackShopDesignCommand: " +
                                    "Quad [0,"+_bufInt+"] is outside the shop!")
                        } else {
                            _result.setWallCoverAtY(_bufInt,fullSpriteName);
                        }
                        break;
                }
            } else {
                //error state: fault to standart
                sendMessage(DebugMessage.ERROR_MESSAGE, "UnpackShopDesignCommand: " +
                        "incorrect cover name '"+bufValue+
                        "' on coords: '"+bufName+"'");
            }
        }
        return _result;
    }
}
}
